<?php


require 'database.php';

$base=new Db;


$page=$_POST['page'];

$text=trim($_POST['text']);


$select=$_POST["select"];

$tid=(int)$_POST["tid"];

$aid=$_POST["aid"];
if($aid){
    $base->doSql('delete from ey_archives where aid="'.$aid.'"');
}

if($page){
    
    if($select){
        
        $___search___=1;
        $__page__=(($page-1)*10);
        if($tid){
            $data=$base->doSql('select * from ey_archives where typeid="'.$tid.'" and title like "%'.$text.'%" limit '.$__page__.',10');
        
        
            $count=$base->doSql('select * from ey_archives where typeid="'.$tid.'" and title like "%'.$text.'%" ');
        }else{
            $data=$base->doSql('select * from ey_archives where typeid=1 and  title like "%'.$text.'%" limit '.$__page__.',10');
        
        
            $count=$base->doSql('select * from ey_archives where typeid=1 and title like "%'.$text.'%" ');
        }
        
        
        for ($i = 0; $i < count($data); $i++) {
            if(!$data[$i]){
                continue;
            }
            $typename=[];
            $typename=$base->doSql('select typename from ey_arctype where id="'.$data[$i]["typeid"].'"');
            $typename=$typename[0];
            $data[$i]["typename"]=$typename["typename"];
            $data[$i]["add_time"]=date('Y-m-d' , $data[$i]["add_time"]);
            
        }
     
    }else{
        $__page__=(($page-1)*10);
        if($tid){
            $data=$base->doSql('select * from ey_archives where typeid="'.$tid.'" limit '.$__page__.',10');
        
        
            $count=$base->doSql('select * from ey_archives where typeid="'.$tid.'" ');
        }else{
            $data=$base->doSql('select * from ey_archives where typeid=1 limit '.$__page__.',10');
        
        
            $count=$base->doSql('select * from ey_archives where typeid=1 ');
        }
        
        
        for ($i = 0; $i < count($data); $i++) {
            if(!$data[$i]){
                continue;
            }
            $typename=[];
            $typename=$base->doSql('select typename from ey_arctype where id="'.$data[$i]["typeid"].'"');
            $typename=$typename[0];
            $data[$i]["typename"]=$typename["typename"];
            $data[$i]["add_time"]=date('Y-m-d' , $data[$i]["add_time"]);
            
        }
    }
    
    
}elseif($text){    
    $___search___=1;
    if($tid){
        $data=$base->doSql('select * from ey_archives where typeid="'.$tid.'" and title like "%'.$text.'%" limit 0,10');
    
    
        $count=$base->doSql('select * from ey_archives where typeid="'.$tid.'" and title like "%'.$text.'%"');
    }else{
        $data=$base->doSql('select * from ey_archives where typeid=1 and title like "%'.$text.'%" limit 0,10');
    
    
        $count=$base->doSql('select * from ey_archives where typeid=1 and title like "%'.$text.'%"');
    }
    
    
    for ($i = 0; $i < count($data); $i++) {
       
        $typename=[];
        $typename=$base->doSql('select typename from ey_arctype where id="'.$data[$i]["typeid"].'"');
        $typename=$typename[0];
        $data[$i]["typename"]=$typename["typename"];
        $data[$i]["add_time"]=date('Y-m-d' , $data[$i]["add_time"]);
        
    }

}elseif(!$page && !$text){
    if($tid){
        $data=$base->doSql('select * from ey_archives where typeid="'.$tid.'" limit 0,10');
    
    
        $count=$base->doSql('select * from ey_archives where typeid="'.$tid.'" ');
    }else{
        $data=$base->doSql('select * from ey_archives where typeid=1 limit 0,10');
    
    
        $count=$base->doSql('select * from ey_archives where typeid=1 ');
    }
    
    
    for ($i = 0; $i < count($data); $i++) {
        if(!$data[$i]){
            continue;
        }
        $typename=[];
        $typename=$base->doSql('select typename from ey_arctype where id="'.$data[$i]["typeid"].'"');
        $typename=$typename[0];
        $data[$i]["typename"]=$typename["typename"];
        $data[$i]["add_time"]=date('Y-m-d' , $data[$i]["add_time"]);
        
    }
}

$__data=[
    "total"    =>count($count),
    "data"=>$data,
    "select"=>$___search___?1:0
];

return json($__data);